package com.yhq.basic.algorithm.array;

import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.Locale;

/**
 * @author hqyin
 * @date 2023/8/22 9:59 上午
 */
public class TestString {

    public static void main(String[] args) {
        //测试反射破坏String不可变
        // test_reflect();
        //测试String +
        //test_StringAdd();
        //
        StringBuilder sb = new StringBuilder();
        sb.append("xx");
        sb.append("b");

        StringBuffer sbf = new StringBuffer();
        sbf.append("xx");
        sbf.append("b");




    }

    private static void test_StringAdd() {
        String abc = "abc";
        String a = "a";
        String bc = "bc";
        String abc1 = "a"+"b"+"c";
        String abc2 = a+bc;
        System.out.println(abc == abc1);
        System.out.println(abc == abc2);
    }

    @SneakyThrows
    private static void test_reflect() {
        String str1 = new String("abc");
        String str = "abc";
        System.out.println("反射操作前");
        System.out.println("str:"+str);
        System.out.println("str1:"+str1);
        System.out.println("str1.intern() == str:"+(str1.intern() == str));
        Field field = str.getClass().getDeclaredField("value");
        field.setAccessible(true);
        field.set(str,new char[] {'d','e','f'});
        System.out.println("反射操作后");
        System.out.println("str1.intern() == str:"+(str1.intern() == str));
        System.out.println("str:"+str);
        System.out.println("str1:"+str1);
    }
}


